package com.zhixiang.wbl.visitor.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zhixiang.wbl.visitor.R;
import com.zhixiang.wbl.visitor.model.MyModel;

/**
 * Created by dongzhixiang on 16/5/19.
 */
public class MyAdapter  extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
    private MyModel[] mDates;
    private Context mContext;
    private LayoutInflater mInflater;
    public OnImageItemClickListener mImageItemClickListener;
    //建立枚举 3个item 类型
    public enum ITEM_TYPE {
        VIEW_TYPE_IMAGE,//
        VIEW_TYPE_TEXT//
    }

    public MyAdapter(MyModel[] mDates, Context mContext)
    {
        this.mDates = mDates;
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    public MyAdapter(MyModel[] mDates, Context mContext,OnImageItemClickListener mListener)
    {
        this.mDates = mDates;
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
        mImageItemClickListener = mListener;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        if (viewType == ITEM_TYPE.VIEW_TYPE_IMAGE.ordinal())
        {
            return new ViewHolderImage(mInflater.inflate(R.layout.template_my_image,parent,false));
        }
        else
        {
            return new ViewHolderText(mInflater.inflate(R.layout.template_my_text,parent,false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
         MyModel myModel = mDates[position];
         if (holder instanceof ViewHolderImage)
         {
             ViewHolderImage viewHolderImage = (ViewHolderImage)holder;
             viewHolderImage.title.setText(myModel.getTitle());
             if (myModel.isFromNet())
             {
                 Picasso.with(mContext).load(myModel.getValue()).placeholder(R.mipmap.main_icon_figure).into(viewHolderImage.image);
             }
             if (myModel.isCanChange())
             viewHolderImage.arrow.setVisibility(View.VISIBLE);
             else
             viewHolderImage.arrow.setVisibility(View.INVISIBLE);
         }
        else
         {
             ViewHolderText viewHolderText = (ViewHolderText)holder;
             viewHolderText.title.setText(myModel.getTitle());
             viewHolderText.value.setText(myModel.getValue());
             if (myModel.isCanChange())
                 viewHolderText.arrow.setVisibility(View.VISIBLE);
             else
                 viewHolderText.arrow.setVisibility(View.INVISIBLE);
         }
    }

    @Override
    public int getItemCount() {
        return mDates.length;
    }

    @Override
    public int getItemViewType(int position)
    {
        if (position<2) return ITEM_TYPE.VIEW_TYPE_IMAGE.ordinal();
        else
            return ITEM_TYPE.VIEW_TYPE_TEXT.ordinal();
    }
    class ViewHolderImage extends RecyclerView.ViewHolder implements View.OnClickListener
    {
        TextView title;
        ImageView image;
        ImageView arrow;
        public ViewHolderImage(View itemView)
        {
            super(itemView);
            title =(TextView)itemView.findViewById(R.id.id_tv_title);
            image = (ImageView)itemView.findViewById(R.id.id_image);
            arrow = (ImageView)itemView.findViewById(R.id.id_right_arrow);
            image.setOnClickListener(this);
        }

        @Override
        public void onClick(View v)
        {
          int position =  getLayoutPosition();
          mImageItemClickListener.onItemClick(v,position);
        }
    }
    class ViewHolderText extends RecyclerView.ViewHolder
    {
       TextView title;
       TextView value;
        ImageView arrow;
        public ViewHolderText(View itemView)
        {
            super(itemView);
            title =(TextView)itemView.findViewById(R.id.id_tv_title);
            value = (TextView)itemView.findViewById(R.id.id_tv_value);
            arrow = (ImageView)itemView.findViewById(R.id.id_right_arrow);
        }
    }

    public interface OnImageItemClickListener
    {
        public void onItemClick(View view,int postion);
    }
}

